package com.example.Controller;

import com.example.entity.Adminusers;
import com.example.service.AdminuserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin")
public class AdminuserCtrl {
    @Autowired
    AdminuserService adminuserService;
    @RequestMapping(value = "/adminusers" ,method = {RequestMethod.GET , RequestMethod.POST})
    public String findAllAdminuser(Model model, @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",required = false, defaultValue = "4") Integer pageSize){
        PageInfo<Adminusers> adminuserList =  adminuserService.queryAll(pageNum,pageSize);
        model.addAttribute("adminuserList",adminuserList);
        return "adminuserList";
    }

    @GetMapping("/delAdminuser/{id}")
    public String delAdminuser(@PathVariable("id") int id, Model model){
        int nu = adminuserService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/admin/adminusers";
    }

    @GetMapping("/adminuser/{id}")
    public String getAdminusersById(@PathVariable("id") int id,Model model){
        Adminusers adminusers = adminuserService.findById(id);
        model.addAttribute("adminusers",adminusers);
        return "adminuserEdit";
    }

    @PostMapping("/editAduminuser")
    public String updateUsers(Adminusers adminusers, Model model){
        int is = adminuserService.update(adminusers);
        model.addAttribute("msg","编辑成功");
        return "forward:/admin/adminusers";
    }

    @GetMapping("/gotoaddAdminuser")
    public String gotoAddadminuser(){
        return "adminuserAdd";
    }

    @PostMapping("/addAdminuser")
    public String addAddminuser(Adminusers adminusers){
        int ainsert = adminuserService.insert(adminusers);
        return "forward:/admin/adminusers";
    }
}
